package com.htjx.mall.commons.models;

import java.util.Date;

import com.htjx.mall.commons.models.enums.SeqType;
import com.htjx.mall.commons.utils.Dates;
import com.htjx.mall.commons.utils.Randoms;

/**
 * 序列号，用于生成ID,不采用数据库自增主键，方便后期扩展
 * 
 * @author jingrenlang
 * 
 */
public class Sequence {

	/** 类型 **/
	private SeqType seq_type;
	/** 描述 **/
	private String name;
	/** 当前值 **/
	private int current_value;
	/** 日期 **/
	private Date last_date;

	public Sequence() {
	}

	public SeqType getSeq_type() {
		return seq_type;
	}

	public void setSeq_type(SeqType seq_type) {
		this.seq_type = seq_type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getCurrent_value() {
		return current_value;
	}

	public void setCurrent_value(int current_value) {
		this.current_value = current_value;
	}

	public Date getLast_date() {
		return last_date;
	}

	public void setLast_date(Date last_date) {
		this.last_date = last_date;
	}

	public static long[] generateSequence(SeqType seqType, int count, int[] seqArray, Date now) {
		long[] retVal = new long[count];
		
		if(seqType == SeqType.Order || seqType == SeqType.Ticket) {
			//订单序号生成规则
			//yyyyMMdd+xxxx+SSS+C  
			// 赠券序号生成规则与订单相同
			for (int i = 0; i < count; i++) {
				String seqStr = String.format("%s%04d%s", Dates.toYYYYMMDD(now), seqArray[i], Randoms.randomNumber(3));
				int chkSum = 0;
				for (int j = 0; j < seqStr.length() ; j++) {
					chkSum += Integer.valueOf(seqStr.substring(j, j+1));
				}
				
				chkSum %=10;
				retVal[i] = Long.valueOf(String.format("%s%01d", seqStr, chkSum));
			}
		} else {
			// 格式化
			for (int i = 0; i < count; i++) {
				retVal[i] = Long.valueOf(String.format("%s%08d", Dates.toYYYYMMDD(now), seqArray[i]));
			}
		}
		
		return retVal;
	}
}
